//
//  ServiceController.swift
//  foreign_swift
//
//  Created by 黄敦仁 on 15/4/22.
//  Copyright (c) 2015年 hdr. All rights reserved.
//

import UIKit
import SnapKit

extension UIView{
    var snp_constraints: [AnyObject] { return self.constraints() }
}

class ServiceController: UIViewController {
    
    var gridWidth:CGFloat = 116
    
    var cellHeight:CGFloat!
    
    var containerView:UIView!
    
    init() {
        super.init(nibName: nil, bundle: nil)
        
        self.title = "服务"
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.edgesForExtendedLayout = UIRectEdge.None
        createScrollView()
        
        addSeparatorLine()
        
        addButtons()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

    }
    
    
    func createScrollView(){
        let realViewHeight = Const.screen_height - self.navigationController!.navigationBar.height - CGFloat(dock_height)
//        println("self.view.height = \(self.view.height) snap_height = \(self.view.snp_height)" )
        if Const.screen_height == 480 {
            let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width:Const.screen_width, height: realViewHeight))
            scrollView.contentSize = CGSize(width: CGFloat(Const.screen_width), height: Const.screen_width / 2 * 3 )
            scrollView.showsVerticalScrollIndicator = false
            scrollView.showsVerticalScrollIndicator = false
            self.view.addSubview(scrollView)
            self.containerView = scrollView
            self.cellHeight = Const.screen_width / 2
        }else{
            self.containerView = self.view
            self.cellHeight = (realViewHeight)/3;
        }

    }
    
    func addSeparatorLine(){
        let lineColor = UIColor.makeWithIntRGB(223, 223, 223)
        

        
        let v1 = UIView(frame: CGRect(x: 0, y: cellHeight, width: Const.screen_width, height: 2))
    
        v1.backgroundColor = lineColor
        containerView.addSubview(v1)
        
        let v2 = UIView(frame: CGRect(x: 0, y: cellHeight*2, width: Const.screen_width, height: 2))
        v2.backgroundColor = lineColor
        containerView.addSubview(v2)
        
        
        let v3 = UIView(frame: CGRect(x: Const.screen_width_center - 1, y: 0, width: 2, height: Const.screen_height))
        v3.backgroundColor = lineColor
        containerView.addSubview(v3)
        
    }
    
    func addButtons(){
        
        let analysisBtn = createButton("分析报告", "Service_analysisReport.png")
        containerView.addSubview(analysisBtn)
        analysisBtn.frame = CGRect(x:0, y: 0, width: gridWidth, height: gridWidth)
        analysisBtn.center = CGPoint(x: Const.screen_width/4, y: cellHeight/2)

        let chartBtn = createButton("图表", "Service_chart.png")
        containerView.addSubview(chartBtn)
        chartBtn.frame = CGRect(x:0, y: 0, width: gridWidth, height: gridWidth)
        chartBtn.center = CGPoint(x: Const.screen_width/4*3, y: cellHeight/2)
        
        let goalBtn = createButton("设置目标", "Service_setGoal.png")
        goalBtn.frame = CGRect(x: 0, y: 0, width: gridWidth, height: gridWidth)
        goalBtn.center = CGPoint(x: Const.screen_width/4, y: cellHeight/2 * 3)
        containerView.addSubview(goalBtn)
        
        let historyBtn = createButton("历史数据", "Service_history.png")
        historyBtn.frame = CGRect(x: 0, y: 0, width: gridWidth, height: gridWidth)
        historyBtn.center = CGPoint(x: Const.screen_width/4*3, y: cellHeight/2 * 3)
        containerView.addSubview(historyBtn)
    }
    
    func createButton(title:String,_ imageName:String)->UIButton{
        let btn = ServiceButton()
        
        let img = UIImage(named: imageName)!
        btn.setImage(img, forState: UIControlState.Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(17)
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.themeColor, forState: UIControlState.Normal)
        
        return btn
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
